﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace Techappy.Silverlight.Toolkit
{
    public class ShowPictureBehavior : Behavior<DependencyObject>
    {
        static int ZIndexListBox = 1;
        static double centerOfRotationZ = 0.0;
        protected override void OnAttached()
        {
            base.OnAttached();
            FrameworkElement el = this.AssociatedObject as FrameworkElement;

            MyPanel panel = el as MyPanel;
            foreach (var item in panel.Children)
            {
                item.MouseEnter += new MouseEventHandler(this.el_MouseEnter);
                item.MouseLeave += new MouseEventHandler(this.el_MouseLeave);
            }
        }

        private void el_MouseLeave(object sender, MouseEventArgs e)
        {
            this.ChangeZIndex(0, sender);
        }

        private void el_MouseEnter(object sender, MouseEventArgs e)
        {
            this.ChangeZIndex(++ZIndexListBox, sender);
        }

        private void ChangeZIndex(int zIndex, object sender)
        {
            DependencyObject parent = sender as DependencyObject;

            //设置元素层次
            Canvas.SetZIndex((FrameworkElement)parent, zIndex);

            FrameworkElement image = parent as FrameworkElement;
            if (centerOfRotationZ == 0.0)
            {
                centerOfRotationZ = (image.Projection as PlaneProjection).CenterOfRotationZ;
            }

            CompositeTransform trans = image.RenderTransform as CompositeTransform;
            Storyboard storyBoard = new Storyboard();

            DoubleAnimation x = new DoubleAnimation() { EasingFunction = new CircleEase() };
            DoubleAnimation y = new DoubleAnimation() { EasingFunction = new CircleEase() };

            y.Duration = new Duration(TimeSpan.FromMilliseconds(300));
            x.Duration = new Duration(TimeSpan.FromMilliseconds(300));

            Storyboard.SetTarget(x, trans);
            Storyboard.SetTarget(y, trans);

            Storyboard.SetTargetProperty(x, new PropertyPath(CompositeTransform.ScaleXProperty));
            Storyboard.SetTargetProperty(y, new PropertyPath(CompositeTransform.ScaleYProperty));

            if (zIndex == 0)
            {
               // (image.Projection as PlaneProjection).CenterOfRotationZ = centerOfRotationZ;
                x.From = 1.05;
                x.To = 1;

                y.From = 1.05;
                y.To = 1;
            }
            else
            {
                 //(image.Projection as PlaneProjection).CenterOfRotationZ = 0;

                x.From = 1;
                x.To = 1.05;

                y.From = 1;
                y.To = 1.05;
            }

            storyBoard.Children.Add(x);
            storyBoard.Children.Add(y);
            storyBoard.Begin();
        }
    }
}
